﻿using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace FileStorage.Controllers
{
    [ApiController]
    [Route("api/Health")]
    public class ApiHealthController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;

        public ApiHealthController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }

        [HttpGet]
        public ActionResult<string> Get(string cmd)
        {
            switch (cmd)
            {
                case "help":
                    var dicHelp = new Dictionary<string, string>
                    {
                        {"version", "show the version of the service"},
                        {"info", "show the service info"},
                        {"config", "show the service current running config"},
                        {"appsettings", "show the .net core running appsettings"}
                    };
                    return Ok(new { status = true, message = JsonConvert.SerializeObject(dicHelp) });
                case "version":
                    return Ok(new { status = true, message = SystemConfig.Version });
                case "info":
                    return Ok(new { status = true, message = "This service is used to for hub file storage service." });
                case "config":
                    var dicConfig = new Dictionary<string, string>()
                    {
                        { "hubReadonlyConnection",SystemConfig.MySqlReadonlyConnection},
                        { "hubConnection",SystemConfig.MySqlConnection},
                        { "uploadPath",SystemConfig.UploadPath},
                        { "version",SystemConfig.Version}
                    };
                    return Ok(new { status = true, message = JsonConvert.SerializeObject(dicConfig) });
                case "appsettings":
                    var rootPath = _webHostEnvironment.ContentRootPath;
                    var filePath = Path.Combine(rootPath, $"appsettings.{_webHostEnvironment.EnvironmentName}.json");
                    if (!System.IO.File.Exists(filePath))
                    {
                        filePath = Path.Combine(rootPath, $"appsettings.json");
                    }
                    return Ok(new { status = true, message = System.IO.File.ReadAllText(filePath) });
                default:
                    return Ok(new { status = true, message = "success" });
            }
        }
    }
}
